package me.zhengjie.pm.projecttask.service.impl;

import me.zhengjie.pm.projecttask.domain.ProjectTask;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projecttask.repository.ProjectTaskRepository;
import me.zhengjie.pm.projecttask.service.ProjectTaskService;
import me.zhengjie.pm.projecttask.service.dto.ProjectTaskDto;
import me.zhengjie.pm.projecttask.service.dto.ProjectTaskQueryCriteria;
import me.zhengjie.pm.projecttask.service.mapper.ProjectTaskMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yuxj
* @date 2020-04-26
*/
@Service
//@CacheConfig(cacheNames = "projectTask")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectTaskServiceImpl implements ProjectTaskService {

    private final ProjectTaskRepository projectTaskRepository;

    private final ProjectTaskMapper projectTaskMapper;

    public ProjectTaskServiceImpl(ProjectTaskRepository projectTaskRepository, ProjectTaskMapper projectTaskMapper) {
        this.projectTaskRepository = projectTaskRepository;
        this.projectTaskMapper = projectTaskMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectTaskQueryCriteria criteria, Pageable pageable){
        Page<ProjectTask> page = projectTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectTaskMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectTaskDto> queryAll(ProjectTaskQueryCriteria criteria){
        return projectTaskMapper.toDto(projectTaskRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectTaskDto findById(Integer id) {
        ProjectTask projectTask = projectTaskRepository.findById(id).orElseGet(ProjectTask::new);
        ValidationUtil.isNull(projectTask.getId(),"ProjectTask","id",id);
        return projectTaskMapper.toDto(projectTask);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectTaskDto create(ProjectTask resources) {
        return projectTaskMapper.toDto(projectTaskRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectTask resources) {
        ProjectTask projectTask = projectTaskRepository.findById(resources.getId()).orElseGet(ProjectTask::new);
        ValidationUtil.isNull( projectTask.getId(),"ProjectTask","id",resources.getId());
        projectTask.copy(resources);
        projectTaskRepository.save(projectTask);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectTaskRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectTaskDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectTaskDto projectTask : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目", projectTask.getProjectId());
            map.put("项目阶段", projectTask.getStageId());
            map.put("分析文件", projectTask.getSourceFile());
            map.put("来源章节和描述信息", projectTask.getSourceSection());
            map.put("计划开始时间", projectTask.getPlanStartTime());
            map.put("计划完成时间", projectTask.getPlanFinishTime());
            map.put("预计工作量", projectTask.getWorkload());
            map.put("开发难度权重", projectTask.getWeight());
            map.put("任务在项目阶段占比", projectTask.getRatio());
            map.put("工作完成状态", projectTask.getWorkState());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}